<?php
class Coupon {
	public $coupon;
	public $site_id;
	public $coupon_sn;
	/** 
	* 参数
	* 当flag=0时id是coupon_id
	* 当flag=1时id是coupon_sn_id
	* 当flag=2时id是coupon_sn表中的sn码
	**/
	function __construct($id,$flag="0"){
		switch ($flag) {
			case "0":
				$map['id'] = $id;
				// dump($id);
				$this->coupon = $coupon = M('coupon')->where($map)->find();
				$this->site_id = $coupon['site_id'];
				break;
			case "1":
				$map['id'] = $id;
				$this->coupon_sn = $coupon_sn = M('coupon_sn')->where($map)->find();
				$map['id'] = $coupon_sn['coupon_id'];
				$this->coupon = $coupon = M('coupon')->where($map)->find();

				$this->site_id = $coupon['site_id'];
				break;
			case "2":
				$map['sn'] = $id;
				$this->coupon_sn = $coupon_sn = M('coupon_sn')->where($map)->find();
				$map = array();//清空数组
				$map['id'] = $coupon_sn['coupon_id'];
				$this->coupon = $coupon = M('coupon')->where($map)->find();

				$this->site_id = $coupon['site_id'];
				break;
			default:
				break;
		}
	}
	// 该优惠券是否可用
	public function canUse($coupon,$coupon_sn){
		// 活动未过期且活动进行中
		$coupon = $coupon ? $coupon : $this->coupon;
		if($coupon['status'] && $coupon['end_time'] > time() && $coupon['start_time'] < time()){
			$coupon_sn = $coupon_sn ? $coupon_sn :$this->coupon_sn;
			if(empty($coupon_sn)){
				// 没有coupon_sn不考虑单张优惠券情况
				return true;
			}else{
				// 优惠券未过期且优惠券未使用
				if($coupon_sn['status'] && (time() < $coupon_sn['ctime'] + $coupon['sn_expires'] * 24 * 3600 || empty($coupon_sn['ctime'])) ){
					return true;
				}
			}
		}
		return false;
	}
	// 下载sn码
	public function download(){
		$map['coupon_id'] = $this->coupon['id'];
        $res = M('coupon_sn')->field('id,sn,order_id,member_id')->where($map)->select();
        $title = array('ID','SN码','订单ID','用户ID');
		exportExcel($res,$title,$this->coupon['title']."优惠券id-sn码");
	}
	// 返回存票数量
	public function exist($coupon){
		$coupon = $coupon ? $coupon : $this->coupon;
		$num = $coupon['num'];

		$map['coupon_id'] = $coupon['id'];
		$map['member_id'] = array('neq','');

		$count = M('coupon_sn')->where($map)->count();
		return $num - $count;
	}

	public function myCoupons($member_id = 0){
		$coupon = $this->coupon;
		if($member_id > 0){
			$map['sn.member_id'] = $member_id;
			$map['sn.site_id'] = $this->site_id;
			$sn = M('coupon_sn')->alias("sn")->where($map)
			->join('__COUPON__ as c on c.id=sn.coupon_id')
			->field('sn.id as id,sn.ctime,c.sn_expires,c.end_time,sn.status,c.title,c.same')->select();

			foreach($sn as &$s){
				$coupon_end_time = $s['end_time'];
				$sn_end_time = $s['ctime'] + $s['sn_expires'] * 24 * 3600;
				if($s['same']){
					$s['end_time'] = $coupon_end_time < $sn_end_time ? $coupon_end_time : $sn_end_time;
				}else{
					$s['end_time'] = $sn_end_time;
				}
				
			}
			return $sn;
		}
		return null;
	}
	// 用户可以获得coupon,只供网页领取类优惠券使用
	public function canTake($member_id = 0,$coupon = ''){
		$coupon = $coupon ? $coupon : $this->coupon;
		if($member_id > 0){
		// canUse 为true,exist > 0 ,领取的票数没有超过额度，该优惠券是网页领取类型(代码1)
			$map['coupon_id'] = $coupon['id'];
			$map['member_id'] = $member_id;
			if($this->canUse() && $this->exist() > 0 && $coupon['type'] == "1" && $coupon['max_num'] > M('coupon_sn')->where($map)->count()){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	public function getCoupon($member_id = 0,$coupon = ''){
		$coupon = $coupon ? $coupon : $this->coupon;
		$map['coupon_id'] = $coupon['id'];
		$map['member_id'] = array('elt', 0);
		$sn = M('Coupon_sn')->where($map)->find();
		if($sn){
			$data['member_id'] = $member_id;
			$data['ctime'] = time();
			$result = M('coupon_sn')->where($sn)->setField($data);
		}else{
			return false;
		}
		
		if($result){
			return $sn['id'];
		}else{
			return false;
		}
	}
	public function setUsed($order_id){
		$sn = $this->coupon_sn;
		$data['use_time'] = time();
		$data['order_id'] = $order_id;
		M('coupon_sn')->where($sn)->setField($data);

	}
	// 发送关注类优惠券，只有关注赠送类优惠券使用
	public function sendSubscribeCoupon($openid = ''){
		if(empty($openid))
			return ;

		$member_map['openid'] = $openid;
		$member = M('member')->where($member_map)->find();
		$map['member_id'] = $member_id = $member['id'];

		$coupon_map['type'] = "3";// 关注赠送类优惠券代码
		$coupon_map['site_id'] = $member['site_id'];
		$coupons = M('coupon')->where($coupon_map)->select();

		foreach($coupons as $coupon){
			$map['coupon_id'] = $coupon['id'];
			if($this->canUse($coupon) 
				&& ($this->exist($coupon) > 0) 
				&& $coupon['type'] == "3" 
				&& $coupon['max_num'] > M('coupon_sn')->where($map)->count()){
				$sn_map['id'] = $this->getCoupon($member_id,$coupon);
				if($sn_map['id']){
					$sn = M('coupon_sn')->where($sn_map)->find();
					$text = "感谢关注，赠送买满".$coupon['full']
					."元可用的价值".$coupon['discount']."元的".$coupon['title']."优惠券";
					sendWXMsg($openid,$text);
				}
			}
		}
	}
	
	
	
	

}